package com.kyg.tjtoolmanager.ui;

import com.kyg.tjtoolmanager.App;
import com.kyg.tjtoolmanager.controller.BaseController;
import com.kyg.tjtoolmanager.db.SQLiteUtils;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TextInputDialog;
import javafx.scene.image.Image;
import javafx.stage.Modality;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;

public class StageUIHelper {

    public static Map<String, Stage> StageMap = new HashMap<>();
    private String title, fxml;

    private static float percent = 0.4f;
    private static boolean equWidth = true;
    private EventHandler<WindowEvent> windowsClosedHandler;
    private Object sendData;
    private static StageUIHelper mInstance;

    public StageUIHelper() {

    }

    public static StageUIHelper getInstance() {
        if (mInstance == null) {
            synchronized (StageUIHelper.class) {
                mInstance = new StageUIHelper();
            }
        }
        return mInstance;
    }

    public void showTipAlert(String msg) {
        showTipAlert(msg, null);
    }

    public void showTipAlert(String msg, Consumer<ButtonType> action) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
                stage.getIcons().add(new Image(App.class.getResourceAsStream(App.icon_launcher)));
                alert.setTitle("提示");
                alert.setContentText(msg);
                Optional<ButtonType> result = alert.showAndWait();
                if (action != null)
                    result.ifPresent(action);
            }
        });
    }

    public void showInputDialog(String msg, Consumer<String> action) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                TextInputDialog dialog = new TextInputDialog("");
                dialog.setTitle("设置");
                Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
                stage.getIcons().add(new Image(App.class.getResourceAsStream(App.icon_launcher)));
                dialog.setContentText(msg + "\n");
                dialog.getEditor().setPromptText("请输入设置密码");
                Optional<String> result = dialog.showAndWait();
                result.ifPresent(action);
            }
        });
    }

    public StageUIHelper setTitle(String title) {
        this.title = title;
        return this;
    }

    public StageUIHelper setFxml(String fxml) {
        this.fxml = fxml;
        return this;
    }

    public StageUIHelper setPercent(float percent) {
        this.percent = percent;
        return this;
    }

    public StageUIHelper setEquWidth(boolean equWidth) {
        this.equWidth = equWidth;
        return this;
    }

    public StageUIHelper setWindowsClosedHandler(EventHandler<WindowEvent> windowsClosedHandler) {
        this.windowsClosedHandler = windowsClosedHandler;
        return this;
    }

    public StageUIHelper setSendData(Object sendData) {
        this.sendData = sendData;
        return this;
    }

    public void show() {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                try {
                    FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml));
                    Scene scene = new Scene(fxmlLoader.load());
                    Stage stage = new Stage();
                    stage.setTitle(title);
                    BaseController controller = fxmlLoader.getController();
                    controller.onBindData(sendData);
//        fxmlLoader.setControllerFactory(new Callback<Class<?>, Object>() {
//        });
//        stage.setAlwaysOnTop(true);
//        stage.setFullScreen(true);
                    Rectangle2D bounds = Screen.getPrimary().getVisualBounds();
                    int width = (int) (bounds.getWidth() * percent);
                    int height = (int) (bounds.getHeight() * percent);
                    stage.setWidth(width);
                    stage.setHeight(equWidth ? width : height);
//        stage.setMaximized(true); //最大化
                    stage.setResizable(false);
                    stage.getIcons().add(new Image(App.class.getResourceAsStream(App.icon_launcher)));
                    stage.setScene(scene);
                    stage.initModality(Modality.APPLICATION_MODAL);
                    if (windowsClosedHandler == null) {
                        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                            @Override
                            public void handle(WindowEvent windowEvent) {
                                StageFactory.dispatchStageClosed(fxml);
                            }
                        });
                    } else {
                        stage.setOnCloseRequest(windowsClosedHandler);
                    }
                    stage.show();
                    StageMap.put(fxml, stage);
                    SQLiteUtils.insertLog(fxml + " show end");
                    StageFactory.dispatchStageShowed(fxml);
                } catch (Exception ex) {
                    SQLiteUtils.insertLog(fxml + " show exception:" + ex.getMessage());
                }
            }
        });
    }

    public static void toCustomUI(String title, String fxml) {
        toCustomUI(title, fxml, 0.5f, equWidth);
    }

    public static void toCustomUI(String title, String fxml, EventHandler<WindowEvent> var1) {
        toCustomUI(title, fxml, 0.5f, equWidth, var1);
    }

    public static void toCustomUI(String title, String fxml, float percent, boolean equWidth) {
        toCustomUI(title, fxml, percent, equWidth, null);
    }

    public static void toCustomUI(String title, String fxml, float percent, boolean equWidth, EventHandler<WindowEvent> var1) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                try {
                    FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml));
                    Scene scene = new Scene(fxmlLoader.load());
                    Stage stage = new Stage();
                    stage.setTitle(title);
                    BaseController controller = fxmlLoader.getController();
//        fxmlLoader.setControllerFactory(new Callback<Class<?>, Object>() {
//        });
//        stage.setAlwaysOnTop(true);
//        stage.setFullScreen(true);
                    Rectangle2D bounds = Screen.getPrimary().getVisualBounds();
                    int width = (int) (bounds.getWidth() * percent);
                    int height = (int) (bounds.getHeight() * percent);
                    stage.setWidth(width);
                    stage.setHeight(equWidth ? width : height);
//        stage.setMaximized(true); //最大化
                    stage.setResizable(false);
                    stage.getIcons().add(new Image(App.class.getResourceAsStream(App.icon_launcher)));
                    stage.setScene(scene);
                    stage.initModality(Modality.APPLICATION_MODAL);
                    if (var1 == null) {
                        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                            @Override
                            public void handle(WindowEvent windowEvent) {
                                StageFactory.dispatchStageClosed(fxml);
                            }
                        });
                    } else {
                        stage.setOnCloseRequest(var1);
                    }
                    stage.show();
                    StageMap.put(fxml, stage);
                    SQLiteUtils.insertLog(fxml + " show end");
                    StageFactory.dispatchStageShowed(fxml);
                } catch (Exception ex) {
                    SQLiteUtils.insertLog(fxml + " show exception:" + ex.getMessage());
                }
            }
        });
    }


}
